﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class backmanage_API_A_Adposition : System.Web.UI.Page
{
    /// <summary>
    /// ADSYSTEM ADPOSITION.API 广告系统API
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        string returnStr = string.Empty;
        string editType = Request.Params["edit"];//操作类型
        switch (editType)
        {
            case "add":
                {
                    returnStr = AdpositionAdd();
                    break;
                }
            case "del":
                {
                    returnStr = AdpositionDel();
                    break;
                }
            case "update":
                {
                    returnStr = AdpositionUpdate();
                    break;
                }
            case "list":
                {
                    returnStr = AdpositionList();
                    break;
                }
            case "state":
                {
                    returnStr = AdpositionUpdateState();
                    break;
                }
            default: break;
        }
        Response.Write(returnStr);
    }
    /// <summary>
    /// 添加广告位
    /// </summary>
    /// <returns></returns>
    protected string AdpositionAdd()
    {
        MODEL.AdPosition model = new MODEL.AdPosition();
        BLL.AdPosition bll = new BLL.AdPosition();
        model.AID = bll.GetMaxId();
        model.Atitle = Request.Params["t"].ToString();
        model.Apage = Request.Params["p"].ToString();
        model.Awidth = Convert.ToInt32(Request.Params["w"]);
        model.Aheight = Convert.ToInt32(Request.Params["h"]);
        model.Astate = Convert.ToBoolean(Convert.ToInt32(Request.Params["s"]));
        return bll.PositionAdd(model).ToString();
    }
    /// <summary>
    /// 删除广告位
    /// </summary>
    /// <returns></returns>
    protected string AdpositionDel()
    {
        string[] idList = Request.Params["id[]"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        string errId = string.Empty;
        for (int i = 0; i < idList.Length; i++)
        {
            BLL.AdPosition bll = new BLL.AdPosition();
            bool flag = bll.PositionDel(idList[i]);
            if (flag)
            {
                errId = "True";
            }
            else
            {
                errId += idList[i] + ",";
            }
        }
        return errId;
    }
    /// <summary>
    /// 修改广告位信息
    /// </summary>
    /// <returns></returns>
    protected string AdpositionUpdate()
    {
        MODEL.AdPosition model = new MODEL.AdPosition();
        BLL.AdPosition bll = new BLL.AdPosition();
        model.AID = Request.Params["id"].ToString();
        model.Apage = Request.Params["p"].ToString();
        model.Astate = Convert.ToBoolean(Convert.ToInt32(Request.Params["s"]));
        model.Atitle = Request.Params["t"].ToString();
        model.Aheight = Convert.ToInt32(Request.Params["h"]);
        model.Awidth = Convert.ToInt32(Request.Params["w"]);
        return bll.PositionUpdate(model).ToString();
    }
    /// <summary>
    /// 修改广告为可用状态
    /// </summary>
    /// <returns></returns>
    protected string AdpositionUpdateState()
    {
        MODEL.AdPosition model = new MODEL.AdPosition();
        BLL.AdPosition bll = new BLL.AdPosition();
        string strBool = Request.Params["s"].ToLower();
        bool flag = false;
        if (strBool == "true")
        {
            flag = false;
        }
        else
        {
            flag = true;
        }
        model.AID = Request.Params["id"].ToString();
        model.Astate = flag;
        return bll.PositionUpdateState(model).ToString();
    }
    /// <summary>
    /// 广告位列表
    /// </summary>
    /// <returns></returns>
    protected string AdpositionList()
    {
        BLL.AdPosition bll = new BLL.AdPosition();
        StringBuilder htmlStr = new StringBuilder();
        DataSet ds = bll.GetAdPositionList();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            htmlStr.Append("<tr id=\"" + ds.Tables[0].Rows[i]["Aid"].ToString() + "\">");
            htmlStr.Append("<td><input type=\"checkbox\" value=\"" + ds.Tables[0].Rows[i]["Aid"].ToString() + "\" /></td>");
            htmlStr.Append("<td><a href=\"#\" class=\"title\" title=\"title\">" + ds.Tables[0].Rows[i]["Atitle"].ToString() + "</a></td>");
            htmlStr.Append("<td><a href=\"#\" class=\"page\" title=\"page\">" + ds.Tables[0].Rows[i]["Apage"].ToString() + "</a></td>");
            htmlStr.Append("<td><a href=\"#\" class=\"size\" title=\"size\"><span class=\"width\">" + ds.Tables[0].Rows[i]["Awidth"].ToString() + "</span>*<span class=\"height\">" + ds.Tables[0].Rows[i]["Aheight"].ToString() + "</span></a></td>");
            htmlStr.Append("<td><a href=\"#\" class=\"state\" title=\"state\">" + ds.Tables[0].Rows[i]["Astate"].ToString().ToLower() + "</a></td>");
            htmlStr.Append("<td><a href=\"#\" class=\"edit\" title=\"edit\">编辑</a></td>");
            htmlStr.Append("</tr>");
        }
        return htmlStr.ToString();
    }
}
